<?php
	require_once( 'core_DataBaseConnect.php' );
	require_once( 'core_ErrorCode.php' );
	require_once( 'core_Response.php' );
	require_once( 'dao_ModelDAO.php' );
	require_once( 'dao_MemberDAO.php' );
	require_once( 'dao_MemberModelDAO.php' );
	require_once( 'dao_ServiceConfigure.php' );
	require_once( '_configs.php' );
	
	function func( $con, $post, $returnType )
	{
		// 기본 정보
		$result = ModelDAO::selectModel( $con, $post[ 'modelCode' ] );
		if( $fetch = DataBaseConnect::fetch_assoc( $result ) )
		{
			$response = DataBaseConnect::fetchSerialize( $fetch, array( 'CODE', 'NAME', 'PICTURE', 'BIRTH_DATE', 'BIRTH_REGION', 'LAST_EDU', 'LAST_EDU_MEMO', 'SEX', 'MILITARY', 'MILITARY_MEMO', 'HEIGHT', 'REF', 'REF_LICENSE' ) );
		}
		
		// 추가 썸네일 정보
		$result = ModelDAO::selectModelThumnails( $con, $post[ 'modelCode' ] );
		$response[ 'thumbnails' ] = array();
		$response[ 'thumbnails' ][ 'thumb' ] = array();
		while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
		{
			array_push( $response[ 'thumbnails' ][ 'thumb' ], DataBaseConnect::fetchSerialize( $fetch, array( 'SEQ', 'LINK' ) ) );
		}
		
		// 평점
		$result = ServiceConfigure::selectStatAVG( $con, $post[ 'modelCode' ] );
		$response[ 'avg' ] = array();
		while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
		{
			array_push( $response[ 'avg' ], DataBaseConnect::fetchSerialize( $fetch, array( 'S_CODE', 'AVG', 'COUNT' ) ) );
		}
		
		if( $post[ 'includeDetail' ] && $post[ 'includeDetail' ] == 'Y' )
		{
			$model_code			= $post[ 'modelCode' ];
			
			// 정당 입력 --> selectModelParty
			$result = ModelDAO::selectModelParty( $con, $model_code );
			$response[ 'party' ] = array();
			while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
			{
				array_push( $response[ 'party' ], DataBaseConnect::fetchSerialize( $fetch, array( 'SEQ', 'START', 'END', 'P_CODE', 'MEMO' ) ) );
			}
			
			
			// 직책 조회 --> selectModelDuty
			$result = ModelDAO::selectModelDuty( $con, $model_code );
			$response[ 'duty' ] = array();
			while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
			{
				array_push( $response[ 'duty' ], DataBaseConnect::fetchSerialize( $fetch, array( 'SEQ', 'START', 'END', 'P_CODE', 'R_CODE', 'D_CODE', 'MEMO', 'COM_CODE' ) ) );
			}
			
			
			// 약력 입력 --> selectModelHistory
			$result = ModelDAO::selectModelHistory( $con, $model_code );
			$response[ 'history' ] = array();
			while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
			{
				array_push( $response[ 'history' ], DataBaseConnect::fetchSerialize( $fetch, array( 'SEQ', 'START', 'END', 'H_CODE', 'MEMO', 'ORDER' ) ) );
			}
			
			
			// 당선 히스토리 --> selectModelElection
			$result = ModelDAO::selectModelElection( $con, $model_code );
			$response[ 'elec' ] = array();
			while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
			{
				array_push( $response[ 'elec' ], DataBaseConnect::fetchSerialize( $fetch, array( 'SEQ', 'DATE', 'G_CODE', 'P_CODE', 'IS_PROP', 'COM_CODE' ) ) );
			}
			
			// 발의 법안 조회 --> selectModelBusiness
			$result = ModelDAO::selectModelBusiness( $con, $model_code );
			$response[ 'business' ] = array();
			while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
			{
				array_push( $response[ 'business' ], DataBaseConnect::fetchSerialize( $fetch, array( 'SEQ', 'M_CODE', 'B_CODE', 'A_CODE', 'B_NAME' ) ) );
			}
			
			// SNS 링크 --> selectModelSns
			$response[ 'sns' ] = array();
			$result = ModelDAO::selectModelSns( $con, $model_code );
			while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
			{
				array_push( $response[ 'sns' ], DataBaseConnect::fetchSerialize( $fetch, array( 'SEQ', 'S_CODE', 'LINK' ) ) );
			}
			// TAG 목록 --> selectModelTags
			$response[ 'tag' ] = array();
			$result = ModelDAO::selectModelTags( $con, $model_code );
			while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
			{
				array_push( $response[ 'tag' ], DataBaseConnect::fetchSerialize( $fetch, array( 'SEQ', 'TAG' ) ) );
			}
			
			// 내가 입력한 능력치 포인트 --> selectMemberModelStat
			// 0. 작성 권한 설정 ( 일반회원 )
			if( MemberDAO::isAccessable( $con, $post[ 'login_member_id' ], array( configs::AUTHOR_ADMIN, configs::AUTHOR_MANAGER, configs::AUTHOR_MEMBER ) ) )
			{
				$response[ 'stat' ] = array();
				try{
					$result = MemberModelDAO::selectModelStat( $con, $post[ 'login_member_id' ], $model_code );
					while( $fetch = DataBaseConnect::fetch_assoc( $result ) )
					{
						array_push( $response[ 'stat' ], DataBaseConnect::fetchSerialize( $fetch, array( 'S_CODE', 'POINT' ) ) );
					}
				}catch( Exception $e ){}
			}
		}
		return Response::SUCCESS( $response, $returnType );
	}
?>